var express = require('express');
const router = express.Router();
const bodyParse=require('body-parser');
var path = require('path');
var mysql = require('mysql');
const routers=require('./router.js');
const fs = require('fs');

var connection=mysql.createConnection({
	host:'localhost',
	user:'root',
	password:'root',
	database:'reward'
});
connection.connect((err)=>{
	if(err) throw err;
	console.log("Connected!");
})
var app=express();
app.use(bodyParse.urlencoded({extended:false}))
app.use(bodyParse.json())
function get_file_content(filepath){
	return fs.readFileSync(filepath)
}
function handle_requser(req,res){
	var suffix = req.url.substr(req.url.length-4,req.url.length);
	let realPath=__dirname+ '\\'+'public'+'\\';
	let fileName = req.url.substr(req.url.length - 9)
	if(suffix === 'css') {
		res.writeHead(200,{'Content-Type':"text/css"});
		res.end(get_file_content(realPath+'\\css'+fileName))
	}
}
const company=require('./company.js');
app.get("/",(req,res)=>{
	let path=__dirname+'/views/index.html';
	res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
	res.end(get_file_content(path))
})
app.listen(3000);
//处理静态资源
app.use((req,res,next)=>{
	console.log("method:"+req.method+",url："+req.url);
	let url = req.url;
	if(/public/.test(url)){
		var path=__dirname+url;
		if(/css/.test(url)){
			res.writeHead(200,{'Content-Type':'text/css'})
			res.end(get_file_content(path))
		}else if(/img/.test(url)){
			if(/png/.test(url)){
				res.writeHead(200,{'Content-Type':'image/png'})
			}else if(/jpg/.test(url)){
				res.writeHead(200,{'Content-Type':'image/jpeg'})
			}
			res.end(get_file_content(path))
		}else if(/js/.test(url)){
			res.writeHead(200,{'Content-Type':'text/javascript;charset=utf-8'})
			res.end(get_file_content(path))
		}
	}
	next()
})
app.use("/about",(req,res,next)=>{
	res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
	if(req.query.id){
		res.end("示例：连续调用了两个中间件")
		next()
	}else{
		console.log("页面没有通过")
	}
	console.log("参数名称：",req.query)
})
app.use("/about.html",(req,res)=>{
	res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
	let path=__dirname+'\\'+"views"+'\\'+'about.html'
	res.end(get_file_content(path))
	console.log("参数名称：",req.query)
})
//处理请求
app.use("/index/get_company",(res,req)=>{
	connection.query('SELECT * FROM company',(err,data)=>{
		if(err) throw err;
		let reqBody={code:0};reqBody.data=data;
		req.writeHead(200,{'Content-Type':'text/json;charset=utf-8'})
		req.end(JSON.stringify(reqBody))
	})
})
app.post("/index/edit_company",(req,res)=>{
	console.log("eidt",req.body)
	if(req.body){
		var param=req.body;
		var sql="UPDATE company SET company = ? WHERE cpid=?"
		connection.query(sql,[param.val,param.id],function(err,result){
			if(err) throw err;
			res.writeHead(200,{'Content-Type':'text/json;charset=utf-8'});
			var responend={code:0,message:'success'}
			res.end(JSON.stringify(responend));
		})
	}
})
app.post("/index/delete_company",(req,res)=>{
	console.log("eidt",req.body)
	if(req.body){
		var param=req.body;
		var sql="DELETE FROM company WHERE cpid = ?"
		connection.query(sql,[param.id],function(err,result){
			if(err) throw err;
			res.writeHead(200,{'Content-Type':'text/json;charset=utf-8'});
			var responend={code:0,message:'success'}
			res.end(JSON.stringify(responend));
		})
	}
})
app.use("/index/add_company",(req,res)=>{
	console.log("eidt",req.body)
	if(req.body){
		var param=req.query;
		var sql="INSERT INTO company(company) VALUES(?)"
		connection.query(sql,[param.company],function(err,result){
			if(err) throw err;
			res.writeHead(200,{'Content-Type':'text/json;charset=utf-8'});
			var responend={code:0,message:'success'}
			res.end(JSON.stringify(responend));
		})
	}
})

